<?php
namespace app\shop\controller;
use app\AdminRest;
use think\App;
use app\shop\model\AdminCoupon as CouponModel;
use app\shop\model\AdminShopType;
use app\shop\model\AdminGoods;
use app\shop\model\CompanyCoupon;
use app\shop\model\UserCoupon;

use think\facade\Cache;
use think\facade\Db;

class AdminCoupon extends AdminRest
{



    protected $model;

    protected $goods_model;

    protected $cate_model;

    protected $company_coupon_model;

    protected $user_coupon_model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model      = new CouponModel();

        $this->goods_model= new AdminGoods();

        $this->cate_model = new AdminShopType();

        $this->company_coupon_model = new CompanyCoupon();

        $this->user_coupon_model = new UserCoupon();
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:54
     * @return \think\Response
     * descrption:福包列表
     */
    public function couponList(){
        $input = $this->_input;
        $dis[]= ['uniacid','=',$this->_uniacid];
        $dis[]= ['status','>',-1];
        if(!empty($this->_input['name'])){
            $dis[] = ['title','like',"%".$this->_input['name']."%"];
        }
        $data = $this->model->couponList($dis,$input['limit']);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-10 19:50
     * @return \think\Response
     * descption:修改福包
     *
     */
    public function couponUpdate(){

        $input = $this->_input;

        $dis   = ['id'=>$input['id']];

        if(count($input)>1){

            if(!empty($input['company_id'])){
                //公司id
                $company_id = $input['company_id'];

                unset($input['company_id']);
            }

            if(isset($input['user_id'])){
                //指定身份的用户
                $user_id = $input['user_id'];

                unset($input['user_id']);
            }

            $input = $this->returnData($input);

            $data  = $this->model->couponUpdate($dis,$input);
            //限制公司部门
            if(!empty($company_id)){
                //公司id集合
                $company_id= $this->company_coupon_model->changeData($company_id);
                //添加
                $this->company_coupon_model->companyCouponAdd($input['id'],$company_id,$this->_uniacid);
            }
            //指定用户
            if(!empty($user_id)){
                //添加指定用户
                $this->user_coupon_model->userCouponAdd($input['id'],$user_id,$this->_uniacid);
            }

        }else{
            $data = $this->model->couponInfo($dis);

            if(!empty($data['limit_goods'])){
                $data['limit_goods'] = explode(',',$data['limit_goods']);
                $diss[] = ['id','in',$data['limit_goods']];
                $data['limit_goods'] = $this->goods_model->goodsId($diss);
            }
            if(!empty($data['limit_cate'])){
                $data['limit_cate'] = explode(',',$data['limit_cate']);
                $data['limit_cate'] = $this->cate_model->limitCate($data['limit_cate']);
            }
            //线下优惠券
            if($data['company_limit']==1){
                //获取该优惠券限用的公司
                $dis = [

                    'a.uniacid'  => $this->_uniacid,

                    'a.coupon_id'=> $data['id'],

                    'a.type'     => 0
                ];
                //每个公司的id和公司名称
                $data['counpon_company'] = $this->company_coupon_model->getId($dis);

            }
            //如果限制了领取人身份
            if($data['get_identity'] ==1){

                $dis = [

                    'uniacid' => $this->_uniacid,

                    'coupon_id'=> $data['id'],

                ];
                //每个用户的id
                $data['coupon_user'] = $this->user_coupon_model->couponUserId($dis);

            }

        }
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-10 19:46
     * @param $data
     * @return mixed
     * descption:调整插入数据库数据
     */
    public function returnData($data){

        if(!empty($data['limit_cate'])){
            $cate = array();
            foreach ($data['limit_cate'] as $v){
                $cate[] = array_pop($v);
            }
            $data['limit_cate'] = $cate;
        }

        if(!empty($data['end_time'])){

            $data['end_time'] = strtotime($data['end_time']);
        }
        if(!empty($data['start_time'])){

            $data['start_time'] = strtotime($data['start_time']);
        }

        if(isset($data['end_time_type'])&&$data['end_time_type']==0&&$data['start_time']>=$data['end_time']){

            $this->errorMsg('到期时间必须大于开始时间');
        }

        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-09-10 18:53
     * @return \think\Response
     * descption:添加福包
     */
    public function couponsAdd(){

        $input = $this->_input;

        if(!empty($input['company_id'])){
            //公司id
            $company_id = $input['company_id'];

            unset($input['company_id']);
        }

        if(isset($input['user_id'])){
            //指定身份的用户
            $user_id = $input['user_id'];

            unset($input['user_id']);
        }

        $input = $this->returnData($input);

        $input['uniacid'] = $this->_uniacid;

        $res  = $this->model->couponAdd($input);
        //优惠券id
        $coupon_id = $this->model->getLastInsID();
        //限制公司部门
        if(!empty($company_id)){
            //公司id集合
            $company_id= $this->company_coupon_model->changeData($company_id);
            //添加
            $this->company_coupon_model->companyCouponAdd($coupon_id,$company_id,$this->_uniacid);
        }
        //指定用户
        if(!empty($user_id)){
            //添加指定用户
            $this->user_coupon_model->userCouponAdd($coupon_id,$user_id,$this->_uniacid);
        }

        return $this->success($res);
    }

    /**
     * 获取限用的商品 限用分类
     */
    public function getGoodsAndCate(){
        $goodsModel     = new AdminGoods();
        $cateModel      = new AdminShopType();
        $dis['uniacid'] = $this->_uniacid;
        $dis['status']  = 1;
        $data['limit_goods']= $goodsModel->goodsSelect($dis);
        $data['limit_cate'] = $cateModel->catSortSelect($dis);
        return $this->success($data);
    }


    /**
     * User: chenniang
     * Date: 2019-09-18 18:38
     * @return void
     * descption:福包分享码
     */
    public function couponShareQr(){
        $input = $this->_param;
        $coupon= $this->model->couponInfo(['id'=>$input['id']]);
        if(empty($coupon)){
            return $this->error('no data');
        }
        $input['pid'] = $input['staff_id'];
        $coupon_qr = getCache($input['id'].'-'.$input['staff_id'].'-coupon_qr');
        if(empty($coupon_qr)){
            $data      = longbingCreateWxCode($this->_uniacid,$input,'shop/pages/coupon/share');
            if(isset($data['qr_path'])) $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);
            $coupon_qr = $data['qr_path'];
            setCache($input['id'].'-'.$input['staff_id'].'-coupon_qr',$coupon_qr);
        }
        return $this->success($coupon_qr);
    }

    /**
     * User: chenniang
     * Date: 2019-09-18 18:42
     * @return void
     * descption:获取所有员工
     */
    public function getStaff(){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['is_staff','=',1];
//        $dis[] = ['company_id','>',0];
//        $dis[] = ['status','',1];
        $data = Db::name('longbing_card_user_info')->where($dis)->field('fans_id,name')->select();
        return $this->success($data);
    }







}
